package com.symria.server.bo.resource;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.List;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import com.symria.framework.page.Page;
import com.symria.framework.util.FileOperate;
import com.symria.framework.util.StringUtil;
import com.symria.server.bo.common.CommonService;
import com.symria.server.dao.resource.ResourceDao;
import com.symria.server.po.AppResource;
import com.symria.system.constants.ATTResource;
import com.symria.system.util.DESUtils;
import com.symria.system.util.FolderManager;
import com.symria.system.util.SessionHelper;

public class ResourceService extends CommonService {

	public ResourceDao resourceDao;
//	private static String targetPath= "D:/谁-小柯";
	private static String targetPath= "/temp";
	private static File fileResource= null;
	private static DESUtils des;
	private static int index = 0;
    
	public static void main(String[] args) {
//		ksc2str("d:/谁-小柯/谁-小柯.ksc","谁-小柯");
//		des = new DESUtils("谁-小柯");
//		ksc2str2();
//		playDes("第三天第三天第三天第三天第三天");
	}
	
	/**
	 * 解析ksc文件
	 * @param path ksc文件
	 * @param name 文件名称
	 */
	public static void ksc2str(File originFile,String name) {
//		des = new DESUtils(name);
		File inFile = new File(originFile.getPath());
		FileInputStream fis = null;
		BufferedReader reader = null;
		File outFile = null;
		BufferedWriter outWriter = null;
		StringBuilder st = new StringBuilder();
		String data = null;
		try {
			fis = new FileInputStream(inFile);
			outFile = new File(targetPath+"/"+name);
			fileResource = new File(targetPath+"/"+name);
			reader = new BufferedReader(new InputStreamReader(fis, "GB2312"));
			FileOutputStream ots = new FileOutputStream(outFile,true);
			outWriter = new BufferedWriter(new OutputStreamWriter(ots, "GB2312"));
			while((data = reader.readLine())!= null) {
				if (!data.contains("karaoke.add")) {
					continue;
				}
				data = data.replace("karaoke.add", "");
				data = data.replace("(", "");
				data = data.replace(");", "");
				data = data.replace("[", "");
				data = data.replace("]", ",");
				data = data.replace("'", "");
				data = data.replace(",,", ",");
				st.append(data);
				st.append("\n");
			}
			System.out.println(st.toString());
			outWriter.write(playDes(st.toString()));
			outWriter.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 解析ksc文件
	 * @param path ksc文件
	 * @param name 文件名称
	 */
	public static void ksc2str2(String path,String name) {
//		des = new DESUtils(name);
//		File inFile = new File(path);
		File inFile = new File("D:/谁-小柯/谁-小柯.ksc");
		FileInputStream fis = null;
		BufferedReader reader = null;
		File outFile = null;
		BufferedWriter outWriter = null;
		StringBuilder st = new StringBuilder();
		String data = null;
		try {
			fis = new FileInputStream(inFile);
			outFile = new File(targetPath+"/"+name);
			fileResource = new File(targetPath+"/"+name+".wrc");
//			outFile = new File("D:/谁-小柯/ABC");
//			outFile = new File("D:/谁-小柯"+"/"+"dddddd");
			reader = new BufferedReader(new InputStreamReader(fis, "GB2312"));
			FileOutputStream ots = new FileOutputStream(outFile,true);
//			outWriter = new BufferedWriter(new OutputStreamWriter(ots, "GB2312"));
			outWriter = new BufferedWriter(new OutputStreamWriter(ots, "GB2312"));
			while((data = reader.readLine())!= null) {
				if (!data.contains("karaoke.add")) {
					continue;
				}
				data = data.replace("karaoke.add", "");
				data = data.replace("(", "");
				data = data.replace(");", "");
				data = data.replace("[", "");
				data = data.replace("]", ",");
				data = data.replace("'", "");
				data = data.replace(",,", ",");
				st.append(data);
				st.append("\n");
			}
			System.out.println(st.toString());
			outWriter.write(playDes(st.toString()));
			outWriter.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public static String playDes(String targetStr){
//		String result = DES.encrypt(targetStr,null);
		String result="";
		try {
			result = encryptDES(targetStr,"12345678");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
//		System.out.println("------------------" + result);
	}
	public static byte[] iv = {1,2,3,4,5,6,7,8};
    public static String encryptDES(String encryptString, String encryptKey) throws Exception {  
//      IvParameterSpec zeroIv = new IvParameterSpec(new byte[8]);   
        /*IvParameterSpec zeroIv = new IvParameterSpec(iv);  
        SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");  
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");  
        cipher.init(Cipher.ENCRYPT_MODE, key);  
        byte[] encryptedData = cipher.doFinal(encryptString.getBytes());  */
    	String result ="";
    	try{
    		result = Base64.encode(encryptString.getBytes());
    	}catch (Exception e) {
    		e.printStackTrace();
		}
       
        return result;
    }
    /**<b>功能说明：检查文件后缀名</b>
	 * 日期：Jul 20, 2010
	 * @author luo
	 * @param fileName
	 * @return
	 */
	public boolean checkSuffix(String fileName,String fixStr){
		if(!StringUtil.isEmpty(fileName)){
		   	String suffix = StringUtil.getExtensionName(fileName);
		   	if(suffix!=null&&fixStr!=null){
			   	String[] strs = fixStr.split(",");
			   	for(String str:strs){
			   		if(StringUtil.equals(str.toLowerCase(), suffix.toLowerCase())) return true;
			   	}
		   	}
		};return false;
	}
	/**
	 * TODO:<B>功能描述:保存文档资源</B>
	 * @param resource
	 * @param resourceFileName
	 * @param name
	 * Administrator--2012-10-31
	 */
	public AppResource saveResource(File resource,String resourceFileName,String name){
		Integer userId = SessionHelper.getSysUser().getId();
		AppResource res = new AppResource();
		String path = FolderManager.getSysUserFolder(userId, resourceFileName);
		String filename=FolderManager.getCurrentName(userId, resourceFileName);
		
		String resPath = FileOperate.uploadFile(resource, path, filename);
		res.setCreateDate(new Date());
		res.setOperId(userId);
		res.setResName(name);
		res.setResSpace(StringUtil.getFileSpace(resource));
		res.setResType(ATTResource.fromPath(resourceFileName, ATTResource.DOC).toString());
	    res.setSuffix(StringUtil.getExtensionName(resourceFileName));
	    res.setResPath(resPath);
	    saveObject(res);
	    return res;
	}
    public void searchResource(Integer uid,String type,String name,Page page){
    	resourceDao.searchResource(uid, type,name,page);
    }
	public ResourceDao getResourceDao() {
		return resourceDao;
	}
	public void setResourceDao(ResourceDao resourceDao) {
		this.resourceDao = resourceDao;
	}
    
}
